/**********************
* Z64MV RDP Processor *
**********************/
#include <stdio.h>
#include <glib.h>
#include <z64mv.h>

/* Function type */
struct ModelTree { int k; };
typedef int (*RDP_HANDLDER)(struct ModelTree *, u8 *, u32, u64);

/* Jump table */
#define SIZE	256
extern RDP_HANDLDER rdp_commands[SIZE];

/* Process a new display list */
static int zgfx_dl ( struct ModelTree * t, u8 * bin, u32 size, u64 i )
{
	u64		next_cmd;
	u64		counter = ZGFX_ADDR(i);
	
process_loop:
	
	/* Too large? */
	if( counter >= size )
		return FALSE;
	
	/* Read next */
	next_cmd = U64(&bin[counter]);
	
	/* Process */
	if( !rdp_commands[ZGFX_INSTR(next_cmd)]( t, bin, size, next_cmd ) )
		
		/* Stop required */
		return TRUE;
	
	/* Keep processing */
	goto process_loop;
}

/* End display list */
static int zgfx_enddl ( struct ModelTree * t, u8 * bin, u32 size, u64 i )
{
	/* Just return false and the parser will act accordingly */
	return FALSE;
}

/* Command not implemented */
static int UNDEFINED ( struct ModelTree * t, u8 * bin, u32 size,  u64 i )
{
	Z64MV_DEBUG( "Undefined RDP instruction caught. Stopping..." );
	return FALSE;
}

/* Begin processing a file at a display list */
int z64mv_preload_begin ( struct ModelTree * t, u8 * bin, u32 size, u32 ep )
{
	zgfx_dl( t, bin, size, U64(&bin[ep]) );
	
	return TRUE;
}

/* Jump table */
RDP_HANDLDER rdp_commands[SIZE] =
{
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, zgfx_dl,   zgfx_enddl,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED,
	UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED
};
